package org.android.tools;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class SSSHelper {
	
	static String NAMESPACE = "http://services.sss.org/";
	static String URL = "http://83.16.166.84:8080/SupportShoppingSystem/SupportShoppingSystemService?WSDL";

	public SSSHelper() {}

	public static Shop[] getShops() {
		String MethodName = "getShops";
		SoapObject response = InvokeMethod(URL,MethodName);
		return Shop.parseShop(response);
	}
	
	public static Product[] getProductsByShop(Shop shop) {
		String MethodName = "getProductsByShop";
		PropertyInfo[] pii = new PropertyInfo[1];
        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("shop");
        pi2.setValue(shop);
        pi2.setType(shop.getClass());
        pii[0] = pi2;
        SoapObject response = InvokeMethod(URL,MethodName,pii);
		return Product.parseProduct(response);
	}
	
	public static int checkUser(long fb_uid) {
		String MethodName = "checkUser";
		PropertyInfo[] pii = new PropertyInfo[1];
		PropertyInfo pi = new PropertyInfo();
        pi.setName("fb_uid");
        pi.setValue(fb_uid);
        pi.setType(Long.class);
        pii[0] = pi;
        SoapObject response = InvokeMethod(URL,MethodName,pii);
        int result = Integer.parseInt(response.getProperty(0).toString());
        return result;
	}
	
	public static ProductShop[] searchProductAndroid(String params, String products) {
		String MethodName = "searchProductAndroid";
		PropertyInfo[] pii = new PropertyInfo[2];
		PropertyInfo pi = new PropertyInfo();
        pi.setName("params");
        pi.setValue(params);
        pi.setType(String.class);
        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("products");
        pi2.setValue(products);
        pi2.setType(String.class);
        pii[0] = pi;
        pii[1] = pi2;
        SoapObject response = InvokeMethod(URL,MethodName,pii);
		return ProductShop.parseProductShop(response);
	}
	
	public static int plusProduct(int idProduct, long fb_uid, int value) {
		String MethodName = "plusProduct";
		PropertyInfo[] pii = new PropertyInfo[3];
		PropertyInfo pi = new PropertyInfo();
        pi.setName("idProduct");
        pi.setValue(idProduct);
        pi.setType(Integer.class);
        pii[0] = pi;
        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("fb_uid");
        pi2.setValue(fb_uid);
        pi2.setType(Long.class);
        pii[1] = pi2;
        PropertyInfo pi3 = new PropertyInfo();
        pi3.setName("value");
        pi3.setValue(value);
        pi3.setType(Integer.class);
        pii[2] = pi3;
        SoapObject response = InvokeMethod(URL,MethodName,pii);
        int result = Integer.parseInt(response.getProperty(0).toString());
        return result;
	}
	
	public static int addNewProduct(long fb_uid, Product product, Shop shop) {
		String MethodName = "addNewProduct";
		PropertyInfo[] pii = new PropertyInfo[3];
		PropertyInfo pi = new PropertyInfo();
        pi.setName("fb_uid");
        pi.setValue(fb_uid);
        pi.setType(Long.class);
        pii[0] = pi;
        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("product");
        pi2.setValue(product);
        pi2.setType(product.getClass());
        pii[1] = pi2;
        PropertyInfo pi3 = new PropertyInfo();
        pi3.setName("shop");
        pi3.setValue(shop);
        pi3.setType(shop.getClass());
        pii[2] = pi3;
        SoapObject response = InvokeMethod(URL,MethodName,pii);
        int result = Integer.parseInt(response.getProperty(0).toString());
		return result;
	}
	
	public static Shop[] getShopByCoordinates(Double x, Double y, String city) {
		String MethodName = "getShopByCoordinates";
		PropertyInfo[] pii = new PropertyInfo[3];
		PropertyInfo pi = new PropertyInfo();
        pi.setName("x");
        pi.setValue(x);
        pi.setType(Double.class);
        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("y");
        pi2.setValue(y);
        pi2.setType(Double.class);
        PropertyInfo pi3 = new PropertyInfo();
        pi3.setName("city");
        pi3.setValue(city);
        pi3.setType(String.class);
        pii[0] = pi;
        pii[1] = pi2;
        pii[2] = pi3;
        SoapObject response = InvokeMethod(URL,MethodName,pii);
		return Shop.parseShop(response);
	}
	
	public static Shop[] getShopByName(String name) {
		String MethodName = "getShopByName";
		PropertyInfo[] pii = new PropertyInfo[1];
		PropertyInfo pi = new PropertyInfo();
        pi.setName("name");
        pi.setValue(name);
        pi.setType(String.class);
        pii[0] = pi;
        SoapObject response = InvokeMethod(URL,MethodName,pii);
		return Shop.parseShop(response);
	}
	
	public static ProductShop[] getRightAmountProductShop(int amount) {
		String MethodName = "getRightAmountProductShop";
		PropertyInfo[] pii = new PropertyInfo[1];
		PropertyInfo pi = new PropertyInfo();
        pi.setName("amount");
        pi.setValue(amount);
        pi.setType(Integer.class);
        pii[0] = pi;
        SoapObject response = InvokeMethod(URL,MethodName,pii);
		return ProductShop.parseProductShop(response);
	}

	public static ProductShop[] getProductShop() {
		String MethodName = "getProductShop";
		SoapObject response = InvokeMethod(URL,MethodName);
		return ProductShop.parseProductShop(response);
	}

	public static String[] getShopName() {
		String MethodName = "getShopName";
		SoapObject response = InvokeMethod(URL,MethodName);
		
		String[] shopsName = new String[response.getPropertyCount()];
		for(int i=0;i<response.getPropertyCount();i++)
		{
			shopsName[i] = response.getProperty(i).toString();
		}
		return shopsName;
	}
	
	public static String[] checkSavedShoppingList(long fb_uid) {
		String MethodName = "checkSavedShoppingList";
		PropertyInfo[] pii = new PropertyInfo[1];
		PropertyInfo pi = new PropertyInfo();
        pi.setName("fb_uid");
        pi.setValue(fb_uid);
        pi.setType(Long.class);
        pii[0] = pi;
        SoapObject response = InvokeMethod(URL,MethodName,pii);
		
		String[] shopsName = new String[response.getPropertyCount()];
		for(int i=0;i<response.getPropertyCount();i++)
		{
			shopsName[i] = response.getProperty(i).toString();
		}
		return shopsName;
	}
	
	public static int saveShoppingList(long fb_uid, String shoppingList) {
		String MethodName = "saveShoppingList";
		PropertyInfo[] pii = new PropertyInfo[2];
		PropertyInfo pi = new PropertyInfo();
        pi.setName("fb_uid");
        pi.setValue(fb_uid);
        pi.setType(Long.class);
        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("shoppingList");
        pi2.setValue(shoppingList);
        pi2.setType(String.class);
        pii[0] = pi;
        pii[1] = pi2;
        SoapObject response = InvokeMethod(URL,MethodName,pii);
        int result = Integer.parseInt(response.getProperty(0).toString());
        return result;
	}

	public static SoapObject InvokeMethod(String URL,String MethodName)
	{
		SoapObject request = GetSoapObject(MethodName);
		SoapSerializationEnvelope envelope = GetEnvelope(request);
		return  MakeCall(URL,envelope,NAMESPACE,MethodName);
	}
	
	public static SoapObject InvokeMethod(String URL,String MethodName, PropertyInfo[] pii)
	{
		SoapObject request = GetSoapObject(MethodName);
		for (int i = 0; i < pii.length; i++) {
			PropertyInfo propertyInfo = new PropertyInfo();
			propertyInfo.setName(pii[i].getName());
			propertyInfo.setValue(pii[i].getValue());
			propertyInfo.setType(pii[i].getType());
			request.addProperty(propertyInfo);
		}
		SoapSerializationEnvelope envelope = GetEnvelope(request);
		return  MakeCall(URL,envelope,NAMESPACE,MethodName);
	}

	public static SoapObject GetSoapObject(String MethodName)
	{
		return new SoapObject(NAMESPACE,MethodName);
	}

	public static SoapSerializationEnvelope GetEnvelope(SoapObject Soap)
	{

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = false;
		envelope.setOutputSoapObject(Soap);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);
		return envelope;
	}

	public static SoapObject MakeCall(String URL, SoapSerializationEnvelope Envelope, String NAMESPACE, String METHOD_NAME)
	{
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
		try
		{
			androidHttpTransport.call(NAMESPACE + METHOD_NAME, Envelope);
			SoapObject response = (SoapObject)Envelope.bodyIn;
			return response;
		}
		catch(Exception e)
		{
			e.printStackTrace();

		}
		return null;
	}


}
